package com.woniu.supermarket.web;

import com.github.pagehelper.PageInfo;
import com.woniu.supermarket.entity.Unit;
import com.woniu.supermarket.result.ResultResponse;
import com.woniu.supermarket.service.IUnitService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/unit")
public class UnitController {
    @Autowired
    IUnitService IUnitService;

    @GetMapping("/ulist")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页", dataType = "int", paramType = "query", name = "now"),
            @ApiImplicitParam(value = "分页单位", dataType = "int", paramType = "query", name = "size"),
            @ApiImplicitParam(value = "按照姓名查询条件", dataType = "String", paramType = "query", name = "unit"),
            @ApiImplicitParam(value = "按照编码查询条件", dataType = "String", paramType = "query", name = "unitCode"),
    })
    public ResultResponse<PageInfo<Unit>> ulist(@RequestParam(value = "now", defaultValue = "1") Integer now,
                                                @RequestParam(value = "size", defaultValue = "3") Integer size,
                                                String cons) {
        ResultResponse<PageInfo<Unit>> result = null;
        try {
            PageInfo<Unit> unitPageInfo = IUnitService.queryByPage(now, size, cons);
            result = new ResultResponse<>(200, "查询编码或名称成功", unitPageInfo);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询编码或名称失败");
        }
        return result;
    }
    //根据id查询单位
    public ResultResponse<Unit> selectUnitByKey(int id){
        ResultResponse<Unit> result = null;
        try {
            Unit unit = IUnitService.queryById(id);
            result = new ResultResponse<>(200, "查询单位成功",unit);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询单位失败");
        }
        return result;
    }

    @PutMapping("/modify")
    @ApiOperation(value = "修改单位数据")
    public ResultResponse<Void> modify(@RequestBody Unit unit) {
        ResultResponse<Void> result = null;
        try {
            int i = IUnitService.modify(unit);
            result = new ResultResponse<>(200, "修改单位成功！");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "修改单位失败！");
        }
        return result;
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加单位数据")
    public ResultResponse<Void> add(@RequestBody Unit unit) {
        ResultResponse<Void> result = null;
        try {
            int i = IUnitService.add(unit);
            result = new ResultResponse<>(200, "添加单位成功！");

        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "修改单位失败！");
        }
        return result;
    }

    @DeleteMapping("/del/{id}")
    @ApiImplicitParam(value = "删除单位 根据id删除", dataType = "int", name = "id", paramType = "path")
    public ResultResponse<Void> del(@PathVariable("id") int id) {
        ResultResponse<Void> result = null;
        try {
            int i = IUnitService.remove(id);
            result = new ResultResponse<>(200, "删除单位成功！");
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "删除单位失败！");
        }
        return result;
    }


    @GetMapping("/unitAllList")
    public ResultResponse<List<Unit>> unitAllList(){
        ResultResponse<List<Unit>> result = null;
        try {
            List<Unit> lists = IUnitService.findAll();
            result = new ResultResponse<>(200, "查询所有单位成功",lists);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询所有单位失败");
        }
        return result;
    }

}
